package com.miao.im.support;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.http.MediaType;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/4/25 09:58
 * @Description:
 */
public class FastJsonConverter extends FastJsonHttpMessageConverter {
    public static final SerializerFeature[] SERIALIZER_FEATURES = new SerializerFeature[]{
            // 将String类型的null转成""
            SerializerFeature.WriteNullStringAsEmpty,
            // 将Number类型的null转成0
            SerializerFeature.WriteNullNumberAsZero,
            // 将List类型的null转成[]
            SerializerFeature.WriteNullListAsEmpty,
            // 将Boolean类型的null转成false
            SerializerFeature.WriteNullBooleanAsFalse,
            //
            SerializerFeature.WriteDateUseDateFormat,
            //SerializerFeature.WriteEnumUsingToString,
            // 避免循环引用`
            SerializerFeature.DisableCircularReferenceDetect
    };
    public FastJsonConverter() {
        super();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SERIALIZER_FEATURES);
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        super.setDefaultCharset(StandardCharsets.UTF_8);
        super.setFastJsonConfig(fastJsonConfig);
        super.setSupportedMediaTypes(supportedMediaTypes);
    }
}
